/**
 * This software is distributed under the terms of the MIT License.
 * Copyright (c) 2020 CrobTech.
 * Author: Jason Zhu
 * Contributors: https://gitee.com/JasonZhu2021/tmf8801_tof_demo
 */

#ifndef TMF8801_REG_H_
#define TMF8801_REG_H_
/**************************************************************************************
 * INCLUDE
 **************************************************************************************/
#include "stdint.h"

/**************************************************************************************
 * CONSTANT DEFINE
 **************************************************************************************/
enum
{
  CALIB_DATA              = 0x0A,
  SERIAL_NUMBER           = 0x47,
  CMD_RESULT              = 0x55,
};

enum{
  UNKNOWN = 0,
  APP_SUCCESS = 1,
  BOOT_SUCCESS,

};

enum{
  BIT_RESET = 0,
  BIT_SET,
};

enum
{
  TMF8801_APP             = 0xC0,
  TMF8801_BOOTLOADER      = 0x80,
};

enum
{
  TMF8801_CPU_READY = 6,
  TMF8801_CPU_RESET = 7,
};

enum Register
{
  TMF8801_APPID          = 0x00,
  TMF8801_APPREQID       = 0x02,

  APPREV_MAJOR           = 0x01,
  APPREV_MINOR           = 0x12,
  APPREV_PATCH           = 0x13,

  CMD_DATA9              = 0x06,
  CMD_DATA8              = 0x07,
  CMD_DATA7              = 0x08,
  CMD_DATA6              = 0x09,
  CMD_DATA5              = 0x0A,
  CMD_DATA4              = 0x0B,
  CMD_DATA3              = 0x0C,
  CMD_DATA2              = 0x0D,
  CMD_DATA1              = 0x0E,
  CMD_DATA0              = 0x0F,
  TMF8801_COMMAND        = 0x10,
  PREVIOUS               = 0x11,
  STATUS                 = 0x1D,
  REGISTER_CONTENTS      = 0x1E,
  TID                    = 0x1F,

  RESULT_NUMBER          = 0x20,
  RESULT_INFO            = 0x21,
  DISTANCE_PEAK_0        = 0x22,
  DISTANCE_PEAK_1        = 0x23,
  SYS_CLOCK_0            = 0x24,
  SYS_CLOCK_1            = 0x25,
  SYS_CLOCK_2            = 0x26,
  SYS_CLOCK_3            = 0x27,
  STATE_DATA_0           = 0x28,
  STATE_DATA_1           = 0x29,
  STATE_DATA_2           = 0x2A,
  STATE_DATA_3           = 0x2B,
  STATE_DATA_4           = 0x2C,
  STATE_DATA_5           = 0x2D,
  STATE_DATA_6           = 0x2E,
  STATE_DATA_7           = 0x2F,
  STATE_DATA_8_XTALK_MSB = 0x30,
  STATE_DATA_9_XTALK_LSB = 0x31,
  STATE_DATA_10_TJ       = 0x32,
  REFERENCE_HITS_0       = 0x33,
  REFERENCE_HITS_1       = 0x34,
  REFERENCE_HITS_2       = 0x35,
  REFERENCE_HITS_3       = 0x36,
  OBJECT_HITS_0          = 0x37,
  OBJECT_HITS_1          = 0x38,
  OBJECT_HITS_2          = 0x39,
  OBJECT_HITS_3          = 0x3A,

  FACTORY_CALIB_0        = 0x20,
  STATE_DATA_WR_0        = 0x2E,

  TMF8801_ENABLE         = 0xE0,
  INT_STATUS             = 0xE1,
  INT_ENAB               = 0xE2,
  TMF8801_WHO_AM_I       = 0xE3,
  REVID                  = 0xE4,
};

enum COMMAND
{
  DISTANCE_MEASURE_MODE_1  = 0x02, /* Set flag to perform target distance measurement with 8 bytes of data containing where including setting of calibration (and algorithm state) configuration. */
  FACTORY_CALIB            = 0x0A, /* Perform factory calibration in the final customer application including cover glass, no ambient light and no target. */
  DOWNLOAD_CALIB_AND_STATE = 0x0B,
};

/**************************************************************************************
 * MCRO DEFINE
 **************************************************************************************/
#define   TMF8801_DEVICE_ADDR   0x82
#define   TMF8801_CHIP_ID       0x07

/**************************************************************************************
 * TYPE DEFINE
 **************************************************************************************/
typedef int32_t (*tmf8801_write_ptr)(void *, uint8_t, uint8_t *, uint16_t);
typedef int32_t (*tmf8801_read_ptr)(void *, uint8_t, uint8_t *, uint16_t);

typedef struct 
{
  tmf8801_write_ptr write_reg;
  tmf8801_read_ptr read_reg;
  void *handle;
}tmf8801_ctx_t;

/**************************************************************************************
 * FUNC DEFINE
 **************************************************************************************/
int32_t tmf8801_device_reset(tmf8801_ctx_t *ctx);
int32_t tmf8801_device_id_get(tmf8801_ctx_t *ctx, uint8_t *buf);
int32_t tmf8801_is_cpu_ready(tmf8801_ctx_t *ctx);
int32_t tmf8801_write_reg(tmf8801_ctx_t *ctx, uint8_t reg, uint8_t data);
int32_t tmf8801_read_reg(tmf8801_ctx_t *ctx, uint8_t reg, uint8_t *data);
int32_t tmf8801_is_app_ready(tmf8801_ctx_t *ctx);
int32_t tmf8801_load_calibdata(tmf8801_ctx_t *ctx, uint8_t *pData, uint16_t size);
int32_t tmf8801_load_algostate(tmf8801_ctx_t *ctx, uint8_t *pData, uint16_t size);
int32_t tmf8801_read_detection_result(tmf8801_ctx_t *ctx, uint8_t *pData);
#endif /* ARDUINO_TMF8801_TMF8801_CONTROL_H_ */
